<template>

  <div class="cards" v-if="true">
    <div class="card hoverBtn">
      <div class="card-title">
        <span>基础信息</span>
        <div class="btnEdit">
          <el-button type="primary" @click="handleEdit()">编辑</el-button>
        </div>


      </div>
      <div class="card-basic">
        <div class="hr-avatar">
          <img :src="avatar" alt="">
        </div>
        <div style="display: flex; align-items: center; " class="txt">
          <p class="hr-name ">{{ initData.hrName.value }}</p>
          <p>
            <el-icon color="#FF69B4  " v-if="initData.hrGender.value == '女'">
              <Female />
            </el-icon>
            <el-icon color="#409EFF" v-if="initData.hrGender.value == '男'">
              <Male />
            </el-icon>
          </p>
        </div>
        <p class="hr-infos txt">
          <span>电话：{{ initData.hrPhone.value }}</span>
          <span style="margin-left: 10px;">邮箱：{{ initData.hrEmail.value }}</span>
        </p>
        <p class="hr-infos txt">
          <span style="margin-right: 2px;">任职：{{ initData.hrEnterprise.value }}</span>
          <!-- <el-divider direction="vertical" />
          <span style="margin-right: 2px;">{{ initData.hrLevel.value }}</span> -->
        </p>
        <div class="hr-detail txt">
          个签：{{ initData.hrSignature.value }}
        </div>
      </div>
    </div>
    <div class="card">
      <div class="card-title"><span>账号编辑</span></div>
      <div class="card-account">
        <el-form ref="fromRef" label-width="100px" :model="upHRPWd" :rules="upHRPWdRules" style="width: 550px;">
          <el-form-item label="用户名">
            <el-input v-model="upHRPWd.name" disabled />
          </el-form-item>
          <el-form-item label="旧密码" prop="oldPwd">
            <el-input v-model="upHRPWd.oldPwd" type="password" />
          </el-form-item>
          <el-form-item label="新密码" prop="password_1">
            <el-input v-model="upHRPWd.password_1" type="password" />
          </el-form-item>
          <el-form-item label="确认密码" prop="password_2">
            <el-input v-model="upHRPWd.password_2" type="password" />
          </el-form-item>


          <el-form-item style=" float: right;">
            <el-button @click="handleReset(fromRef)">重置</el-button>
            <el-button type="primary" @click="handleConfirm(fromRef)">提交</el-button>
          </el-form-item>
        </el-form>

      </div>
    </div>
  </div>
  <HRUpInfo v-if="dialogFormVisible" :dialogFormVisible="dialogFormVisible" :handleCloseDialog="handleCloseDialog" />

</template>

<script lang='ts' setup>
import HRUpInfo from "@/components/enterprise/einfo/HRUpInfo.vue"
import { Female, Male } from '@element-plus/icons-vue'
import { getEHRInfo, upEHRInfo, upEHRPwd } from "@/api/enterpriseApi"
import { loginOu } from "@/api/authApi"
import { upHRPWd, upHRPWdRules } from "@/utills/enterpriseValidators"
import { elmessage,data } from "@/hook/hook"
import { reactive, ref, onBeforeMount, watch, computed, onMounted } from "vue"
import type { FormInstance, FormRules } from 'element-plus'
import { useStore } from "vuex"
import { useRouter } from "vue-router"

const store = useStore()
const router = useRouter()
const avatar = ref()
const isShow = ref(false)
const fromRef = ref<FormInstance>()
const dialogFormVisible = ref(false)

const hr = store.state.hrState.hr

const handleEdit = () => {
  dialogFormVisible.value = true
}
const handleCloseDialog = () => {
  dialogFormVisible.value = false
}


// 重置-账号编辑
const handleReset = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
// 提交-账号编辑
const handleConfirm = async (formEl: FormInstance | undefined) => {

  if (!formEl) return
  await formEl.validate(async (valid: any) => {
    if (valid) {
      const newData = {
        ...data(),
        oldPwd: upHRPWd.oldPwd,
        newPwd: upHRPWd.password_1
      }
      await upEHRPwd(newData).then((res: any) => {
        if (res.data["code"] == "200") {
          elmessage(res.data["message"], res.data["type"])
          setTimeout(() => {
            router.replace("/login")
          }, 500);
          loginOu(data())
          localStorage.clear()
        }
        else if (res.data["code"] == "300") {
          elmessage(res.data["message"], res.data["type"])
          setTimeout(() => {
            router.replace("/login")
          }, 500);
          localStorage.clear()
        }
        else if (res.data["code"] == "501") {
          elmessage(res.data["message"], res.data["type"])
        }
        else {
          console.log(res.data);

        }
      })
      formEl.resetFields()
    } else {
      return false;
    }
  });


}

const initData = {
  hrId: computed(() => hr.hrId),
  hrName: computed(() => hr.hrName),
  hrGender: computed(() => hr.hrGender),
  hrEmail: computed(() => hr.hrEmail),
  hrPhone: computed(() => hr.hrPhone),
  hrEnterprise: computed(() => hr.hrEnterprise),
  hrLevel: computed(() => hr.hrLevel),
  hrSignature: computed(() => hr.hrSignature),
}



const init = () => {
 
  
  getEHRInfo(data()).then((res: any) => {
    if (res.data["code"] == "200") {
      hr.hrId = res.data["hrId"]
      hr.hrName = res.data["hrName"]
      hr.hrGender = res.data["hrGender"]
      hr.hrEmail = res.data["hrEmail"]
      hr.hrPhone = res.data["hrPhone"]
      hr.hrEnterprise = res.data["hrEnterprise"]
      hr.hrLevel = res.data["hrLevel"]
      hr.hrSignature = res.data["hrSignature"]
      upHRPWd.name = res.data["hrName"]
      isShow.value = true
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data);

    }

  })
}



// 获取头像
const watchVuexName = computed(() => hr.hrName)
watch(watchVuexName, (newval, oldval) => {
  upHRPWd.name = hr.hrName
}, { immediate: true })
const watchVuexAvatar = computed(() => hr.hrAvatarName)
watch(watchVuexAvatar, (newval, oldval) => {
  avatar.value = hr.hrAvatarName
}, { immediate: true })

onMounted(() => {
  initData

})
onBeforeMount(() => {
  init()
  

  //  initData

})

</script>
<style scoped>
.hoverBtn:hover .btnEdit {
  display: block;
}

.btnEdit {
  float: right;
  line-height: 60px;
  margin-right: 60px;
  box-sizing: border-box;
  display: none;
  /* margin: 0 auto; */
}

.hr-infos {
  display: flex;
  line-height: 24px;
  align-items: center;
}

.txt {
  margin-bottom: 12px;
  margin-left: 110px;
}

.card-basic {
  padding-top: 30px;
  padding-left: 20px;
}

.hr-detail {
  font-size: 16px;
}

.card-account {
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.hr-name {
  font-size: 30px;
  margin-right: 5px;
  /* margin-bottom: 12px; */

}

.card-title {
  position: relative;
  padding-left: 20px;
  line-height: 60px;
  border-bottom: 1px solid #e4e7ed;
  font-size: 17px;


}

/* HR头像 */
.hr-avatar {
  /* margin-top: 30px; */
  position: absolute;
  margin-top: 10px;

}

.hr-avatar img {
  width: 90px;
  height: 90px;
  border-radius: 100%;
}

.card {
  width: 600px;
  height: 330px;
  background-color: #FFF;
  border: 1px solid #e4e7ed;
  border-radius: 10px;
  /* float: left; */

  /* left: 50px; */
}

.card:hover {

  transition: border linear .2s;
  box-shadow: 0 0 10px rgba(213, 208, 213, 3);

}

.cards {
  min-width: 900px;
  display: flex;
  box-sizing: border-box;
  justify-content: space-around;
  align-content: center;
}
</style>